<?php

namespace App\Traits\Relation;

use App\Models\Audit;
use App\Models\Career;
use App\Models\Permission;
use App\Models\Project;
use App\Models\Company;
use App\Models\Personal;
use App\Models\Position;
use App\Models\Schedule;
use App\Models\Department;
use App\Models\Attendance;
use App\Models\Identification;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * 用于定义用户和其他模型的关系
 *
 * @package App\Traits\Relation
 * Date: 19/03/2018
 * @author George
 */
trait UserRelation
{
	/**
	 * 获取用户的职业生涯信息
	 *
	 * Date: 19/03/2018
	 * @author George
	 * @return Relation
	 */
	public function careers()
	{
		return $this->hasMany(Career::class, 'user_id', 'id');
	}

	/**
	 * 获取用户身份证信息
	 *
	 * Date: 19/03/2018
	 * @author George
	 * @return Relation
	 */
	public function identificationInfo()
	{
		return $this->hasOne(Identification::class, 'identification', 'identification');
	}

	/**
	 * 获取用户个人信息
	 *
	 * Date: 19/03/2018
	 * @author George
	 * @return HasOne
	 */
	public function personal()
	{
		return $this->hasOne(Personal::class, 'identification', 'identification');
	}

	/**
	 * 获取用户的公司信息
	 *
	 * Date: 25/03/2018
	 * @author George
	 * @return HasOne
	 */
	public function company()
	{
		return $this->hasOne(Company::class, 'id', 'company_id');
	}

	/**
	 * 获取用户入职过的公司
	 *
	 * Date: 05/05/2018
	 * @author George
	 * @return BelongsToMany
	 */
	public function companies()
	{
		return $this->belongsToMany(Company::class, 'company_user_pivot', 'user_id', 'company_id', 'id', 'id')->withPivot('hiredate_at', 'departure_at', 'score', 'comment');
	}

	/**
	 * 关联人员主项目
	 *
	 * Date: 12/04/2018
	 * @author George
	 * @return HasOne
	 */
	public function project()
	{
		return $this->hasOne(Project::class, 'id', 'project_id');
	}

	/**
	 * 获取用户所属项目信息
	 *
	 * Date: 25/03/2018
	 * @author George
	 * @return BelongsToMany
	 */
	public function projects()
	{
		return $this->belongsToMany(Project::class, 'user_project_pivot', 'user_id', 'project_id', 'id', 'id')->withPivot('company_id', 'type', 'start', 'end');
	}

	/**
	 * 获取用户的岗位信息
	 *
	 * Date: 25/03/2018
	 * @author George
	 * @return HasOne
	 */
	public function position()
	{
		return $this->hasOne(Position::class, 'id', 'position_id');
	}

	/**
	 * 获取用户的排班信息
	 *
	 * Date: 16/04/2018
	 * @author George
	 * @return HasMany
	 */
	public function schedules()
	{
		return $this->hasMany(Schedule::class, 'user_id', 'id');
	}

    /**
     * 获取用户考勤记录
     *
     * Date: 23/04/2018
     * @author George
     * @return HasMany
     */
    public function attendances()
    {
        return $this->hasMany(Attendance::class, 'user_id', 'id');
	}

	/**
	 * 获取用户申请
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @return HasMany
	 */
	public function joins()
	{
		return $this->hasMany(Audit::class, 'auditable_id', 'id');
	}

	/**
	 * 获取用户所在部门
	 *
	 * Date: 2018/5/24
	 * @author George
	 * @return mixed
	 */
	public function department()
	{
		return $this->belongsTo(Department::class, 'department_id', 'id');
	}

    /**
     * 定义人员与权限的关系
     *
     * Date: 22/03/2018
     * @author George
     * @return BelongsToMany
     */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class, 'user_permission_pivot', 'user_id', 'permission_code', 'id', 'code');
    }

}
